export const calcDate = (date1, date2) => {
  let date3 = date2 - date1;

  let days = Math.floor(date3 / (24 * 3600 * 1000))

  let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
  let hours = Math.floor(leave1 / (3600 * 1000))

  let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
  let minutes = Math.floor(leave2 / (60 * 1000))

  let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
  let seconds = Math.round(date3 / 1000)
  return {
    leave1,
    leave2,
    leave3,
    days: days,
    hours: hours,
    minutes: minutes,
    seconds: seconds,
  }
}

/**
 * 日期格式化
 */
export function dateFormat(date, format) {
  format = format || 'yyyy-MM-dd hh:mm:ss';
  if (date !== 'Invalid Date') {
    let o = {
      "M+": date.getMonth() + 1, //month
      "d+": date.getDate(), //day
      "h+": date.getHours(), //hour
      "m+": date.getMinutes(), //minute
      "s+": date.getSeconds(), //second
      "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
      "S": date.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (let k in o)
      if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
          RegExp.$1.length === 1 ? o[k] :
            ("00" + o[k]).substr(("" + o[k]).length));
    return format;
  }
  return '';
}

/**
 * 获取当前日期
 */
export function getNowDate() {
  let now = new Date();
  let year = now.getFullYear(); //得到年份
  let month = now.getMonth(); //得到月份
  let date = now.getDate(); //得到日期
  month = month + 1;
  month = month.toString().padStart(2, "0");
  date = date.toString().padStart(2, "0");
  let defaultDate = year+'-'+month+'-'+date;
  return defaultDate;
}

/**
 * 获取当前日期+时间
 */
export function getNowTime() {
  let now = new Date();
  let year = now.getFullYear(); //得到年份
  let month = now.getMonth(); //得到月份
  let day = now.getDate(); //得到日期
  let hour = now.getHours();//得到小时
  let minu = now.getMinutes();//得到分钟
  let sec = now.getSeconds();//得到秒
  month = month + 1;
  month = month.toString().padStart(2, "0");
  hour = hour.toString().padStart(2, "0");
  minu = minu.toString().padStart(2, "0");
  sec = sec.toString().padStart(2, "0");
  day = day.toString().padStart(2, "0");
  let defaultDate = year+'-'+month+'-'+day+' '+hour+':'+minu+':'+sec;
  return defaultDate;
}

//获取第一个月第一天
export function getCurrentMonthFirstDay() {
    var date = new Date()
    date.setDate(1)
    var month = parseInt(date.getMonth() + 1)
    var day = date.getDate()
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    return date.getFullYear() + '-' + month + '-' + day
}

//获取最后一天
export function getCurrentMonthLastDay() {
  var date = new Date()
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  month = month < 10 ? '0' + month : month
  var day = new Date(year, month, 0)
  return  year + '-' + month + '-' + day.getDate()
}

/** 
 * 获取当月的天数
*/
export function getCountDays(dateStr) {
  var curDate = new Date(dateStr);
  /* 获取当前月份 */
  var curMonth = curDate.getMonth();
  /*  生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
  curDate.setMonth(curMonth + 1);
  /* 将日期设置为0 */
  curDate.setDate(0);
  /* 返回当月的天数 */
  return curDate.getDate();
}

/**
 * 获取昨日日期
 */
export function getYesterday() {
  let now = new Date();
  now.setTime(now.getTime() - 24 * 60 * 60 * 1000);
  let year = now.getFullYear(); //得到年份
  let month = now.getMonth(); //得到月份
  let date = now.getDate(); //得到日期
  month = month + 1;
  month = month.toString().padStart(2, "0");
  date = date.toString().padStart(2, "0");
  let defaultDate = year+'-'+month+'-'+date;
  return defaultDate;
}

/**
 * 日期加减
 * @param date 被加或减的日期
 * @param days 需要加或减的数字
 * @returns {date}
 */
export function addDate(date ,days){
  days = days||0;
  return addHour(days*24,date);
}

/**
 * 日期加减--小时
 */
export function addHour(hours,date){
  date = date||new Date();
  if(date instanceof Date){
  }else{
    date = new Date(date);
  }
  var date_s = date.getTime();//转化为时间戳毫秒数
  return dateFormat(new Date(date.setTime(date_s + 1000 * 60 * 60 * hours)));
}

/** yyyyMMddHHmmss 格式的时间转为日期 */
export function tsGetDate(dateString) {
  var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
  var formatedDate = dateString.replace(pattern, '$1/$2/$3 $4:$5:$6');
  return new Date(formatedDate);
}
